import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: syyyy
 * Date: 2025-08-02
 * Time: 17:02
 */
public class MySingleList {
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val=val;
        }
    }

    public ListNode head;

    public void addFirst(int data){
        ListNode node=new ListNode(data);

        node.next=head;

        head=node;
    }


    public void removeAllKey(int key){
        ListNode cur=head;
        ListNode prev=head.next;

        if(head==null){
            return;
        }

        while(prev!=null) {
            if (prev.val == key) {
                cur.next = prev.next;
                prev = prev.next;
            } else {
                prev = prev.next;
                cur = cur.next;
            }
        }

        if(head.val==key){
            head=head.next;
        }

    }

    public ListNode partition(int x){
        ListNode cur=head;
        ListNode as=null;
        ListNode ae=null;
        ListNode bs=null;
        ListNode be=null;

        while(cur!=null){
            if(cur.val<x){
                if(as==null){
                    as=ae=cur;
                }else{
                    ae.next=cur;
                    ae=ae.next;
                }
            }else{
                if(bs==null){
                    bs=be=cur;
                }else{
                    be.next=cur;
                    be=be.next;
                }
            }
            cur=cur.next;
        }
        if(as==null){
            return bs;//这里不需要写be.next=null，因为这个题目不需要排序，所以把原来的照搬的话null还是在最后一个
        }else {
            ae.next=bs;
        }

        if(bs != null) {
            be.next = null;
        }
        return as;
    }

    public boolean chkPalindrome(){
        if(head==null){
            return true;
        }

        ListNode fast=head;
        ListNode slow=head;

        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        ListNode cur=slow.next;
        while(cur!=null){
            ListNode curN=cur.next;
            cur.next=slow;
            slow=cur;
            cur=curN;
        }


        while (head!=slow){//不可以用fast代替slow，因为当节点个数为偶数时，fast为null了
            if(slow.val!= head.val){
                return false;
            }
            if(head.next==slow){//当节点个数为偶数时
                return true;
            }
            slow=slow.next;
            head=head.next;
        }

        return true;
    }

    public ListNode getIntersectionNode(ListNode headA,ListNode headB){

        int l1=0;
        int l2=0;
        ListNode aa=headA;
        ListNode bb=headB;

        while(aa!=null){
            l1++;
            aa=aa.next;
        }
        while(bb!=null){
            l2++;
            bb=bb.next;
        }

        aa=headA;
        bb=headB;
        int len=l1-l2;
        int count=0;
        if(len<0){
            while(count!=len){
                bb=bb.next;
                count++;
            }
        }else{
           while(count!=len){
               aa=aa.next;
               count++;
           }
        }

        while (aa.next!=bb.next){
            aa=aa.next;
            bb=bb.next;
        }
        return aa.next;
    }

    public boolean hasCycle(){
        ListNode fast=head;
        ListNode slow=head;

        while(fast!=null&&fast.next!=null){//为什么不写fast！=slow这个条件？答：如果没有环形，fast永远不会等于slow
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
                return true;
            }
        }
        return false;
    }

    public ListNode detectCycle(){
        ListNode fast=head;
        ListNode slow=head;

        while(fast!=null&&fast.next!=null){//为什么不写fast！=slow这个条件？答：如果没有环形，fast永远不会等于slow
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
                break;
            }
        }
        if(fast==null||fast.next==null){
            return null;//没有环
        }
        slow=head;
        while(slow!=fast){
            slow=slow.next;
            fast=fast.next;
        }
        return slow;
    }



    public void display2(ListNode newhead){
        ListNode cur=newhead;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }

    public void display(){
        ListNode cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }

    public int size() {
        int len = 0;
        ListNode cur = head;
        while (cur != null) {
            len++;
            cur = cur.next;
        }
        return len;
    }
}
